<?php
namespace app\admin\controller\sys;

use app\admin\model\AdminRule;
use app\admin\service\sys\RuleService;
use app\common\controller\AdminBase;

/**
 * 后台菜单/权限操作控制器
 * 列表 添加 修改 删除等
 */
class Rule extends AdminBase{
    /**
     * 后台菜单/权限 列表
     */
    public function rule_list(){

        $parmas = reqDecode();//获取参数及解密

        $model = new AdminRule();
        $parmas['pid'] = 0;
        $list = $model->withSearch($this->searchFields($parmas),$this->searchData($parmas))->field('id,title,path,type,state,pid,icon,sort')
            ->order('id','ASC')
            ->paginate($parmas['limit'], false)->toArray();

        if(!empty($list['data'])){
            $re = RuleService::ruleMenuList(); //获取全部子下级菜单
            foreach ($list['data'] as $key=>$vs){
                $list['data'][$key]['children'] =  $this->getTreeOrder($re,$vs['id']); //根据PID给子菜单 递归分级
            }
        }

//        print_r($list);exit;

        return suc('成功',$list);
    }

    /**
     *  后台菜单/权限 添加
     */
    public function rule_add(){
        $parmas = reqDecode();//获取参数及解密
        if(!empty($parmas) && !empty($parmas['title'])){
            $res =  RuleService::addRuleMenu($parmas);
            if($res){
                return suc('成功',$res);
            }
        }
        return suc('非法操作','',1);
    }

    /**
     *  后台菜单/权限 父级菜单列表
     *  二级选择 用于联级
     * 用于菜单添加选择
     */
    public function rule_arr(){
        $res =  RuleService::getRuleArr();
        return suc('成功',$res);
    }


    /**
     *  全部菜单 递归分类
     * 使用于 角色权限选择
     * 无限级
     */
    public function role_rule_arr(){
        $res =  RuleService::getRoleRuleArr();
        $re = RuleService::ruleMenuList(); //获取全部子下级菜单
        foreach ($res as $key=>$vs){
            $res[$key]['children'] =  $this->getTreeOrder($re,$vs['id']); //根据PID给子菜单 递归分级
        }
        return suc('成功',$res);
    }
}
